#include "PointLight.h"
#include "../Raycaster/Assistant.h"

namespace MASGK
{
	// _point is a center of triangle (lamba1 = lambda2 = lambda3 = 1/3)
	float3 CPointLight::PointLightEquation(float3 _point)
	{
		float3 distanceV = _point - (*m_Position);
		float distance = distanceV.length();
		float denom;

		if (FEQUAL(m_Attenuation.constant, 0.0f) && FEQUAL(m_Attenuation.linear, 0.0f) && FEQUAL(m_Attenuation.exponent, 0.0f))
			return float3(0.0f, 0.0f, 0.0f);

		denom = m_Attenuation.constant + m_Attenuation.linear * distance + m_Attenuation.exponent * distance * distance;

		return float3(1/denom * (*m_Color).r, 1/denom * (*m_Color).g, 1/denom * (*m_Color).b);
	}

	// ks and kd == 1 for now...
	float3 CPointLight::Phong(float3 _L, float3 _pos, float3 _N, int _n)
	{
		float3 L = (*m_Position) - _pos;
		float3 R = L.reflect(_N);
		float3 V = float3(0, 0, 1.0f);		// TEMP!!!
		float kd = 0.5f;
		float ks = 17.0f;

		L.normalizeMe();
		R.normalizeMe();
		V.normalizeMe();
		_N.normalizeMe();

		return _L * (kd * dot(L, _N) + ks * pow(dot(R, V), _n * 1.0f));
	}
}